package com.imedway.ihe.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.ProvideAndRegisterDocumentSetRequestType;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.ProvideAndRegisterDocumentSetRequestType.Document;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.AssociationType1;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ClassificationType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ExtrinsicObjectType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.IdentifiableType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.RegistryObjectListType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.RegistryPackageType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.SlotType1;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ValueListType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
import org.openehealth.ipf.commons.ihe.xds.iti41.Iti41PortType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.imedway.ihe.dtz.xds.cons.ConstProperties;
import com.imedway.ihe.dtz.xds.cons.SysConsts;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dtz<br/>
 * @date 2024/7/3
 */
@Slf4j
@RestController
@RequestMapping("iti41")
public class Iti41PortTypeClient {

	@Autowired
	private ConstProperties constProperties;
	
    @SuppressWarnings("unchecked")
	@PostMapping
    public Object iti41() throws MalformedURLException {
        URL wsdlUrl = new URL("http://localhost:8080/services/iti41?wsdl");
        QName serviceName = new QName("http://service.ihe.imedway.com/", "Iti41PortTypeImplService");
        Service service = Service.create(wsdlUrl, serviceName);
        Iti41PortType myWebService = service.getPort(Iti41PortType.class);
        ProvideAndRegisterDocumentSetRequestType body = new ProvideAndRegisterDocumentSetRequestType();
        SubmitObjectsRequest submitObjectsRequest = new SubmitObjectsRequest();
                
        RegistryObjectListType registryObjectList = new RegistryObjectListType();
        
        List<JAXBElement<? extends IdentifiableType>>  registryObjectTypeList = registryObjectList.getIdentifiable();
        
        //ExtrinsicObject
        ExtrinsicObjectType extrinsicObjectType = new ExtrinsicObjectType();
        extrinsicObjectType.setId("Document01");
        extrinsicObjectType.setMimeType("text/plain");
//    	text/plain 表示文本文件的默认值。
//    	application/octet-stream 表示所有其他情况的默认值。
//        超文本标记语言文本 .html、.html：text/html
//        普通文本 .txt： text/plain
//        RTF 文本 .rtf： application/rtf
//        GIF 图形 .gif： image/gif
//        JPEG 图形 .jpeg、.jpg： image/jpeg
//        au 声音文件 .au： audio/basic
//        MIDI 音乐文件 mid、.midi： audio/midi、audio/x-midi
//        RealAudio 音乐文件 .ra、.ram： audio/x-pn-realaudio
//        MPEG 文件 .mpg、.mpeg： video/mpeg
//        AVI 文件 .avi： video/x-msvideo
//        GZIP 文件 .gz： application/x-gzip
//        TAR 文件 .tar： application/x-tar
        
        extrinsicObjectType.setObjectType("urn:uuid:7edca82f-054d-47f2-a032-9b2a5b5186c1");
        List<ClassificationType> classification0 = new ArrayList<ClassificationType>();
        extrinsicObjectType.getClassification().addAll(classification0);
        JAXBElement<ExtrinsicObjectType> extrinsicObjectTypeJAXBElement= 
        		new JAXBElement<ExtrinsicObjectType>(new QName("urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0","ExtrinsicObject"), 
        				(Class<ExtrinsicObjectType>) extrinsicObjectType.getClass(), extrinsicObjectType);
        registryObjectTypeList.add(extrinsicObjectTypeJAXBElement);
    
        //RegistryPackage
        RegistryPackageType registryPackageType = new RegistryPackageType();
        registryPackageType.setId("SubmissionSet01");
        JAXBElement<RegistryPackageType> registryPackageTypeJAXBElement= 
        		new JAXBElement<RegistryPackageType>(new QName("urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0","RegistryPackage"), 
        				(Class<RegistryPackageType>) registryPackageType.getClass(), registryPackageType);
        registryObjectTypeList.add(registryPackageTypeJAXBElement);
        
        //Classification
        ClassificationType classificationType = new ClassificationType();
        classificationType.setId("cl10");
        classificationType.setClassifiedObject("SubmissionSet01");
        classificationType.setClassificationNode("urn:uuid:a54d6aa5-d40d-43f9-88c5-b4633d873bdd");
        JAXBElement<ClassificationType> registryObjectTypeJAXBElement= 
        		new JAXBElement<ClassificationType>(new QName("urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0","Classification"),
        				(Class<ClassificationType>) classificationType.getClass(), classificationType);

        
        registryObjectTypeList.add(registryObjectTypeJAXBElement);
        
        //Association
        AssociationType1 associationType1 = new AssociationType1();
        associationType1.setAssociationType("HasMember");
        associationType1.setId("as01");
        associationType1.setSourceObject("SubmissionSet01");
        associationType1.setTargetObject("Document01");
        List<SlotType1> slotList = new ArrayList<SlotType1>();
        SlotType1 slotType1 = new SlotType1();
        
        
        
        ValueListType valueList =new ValueListType();
        List<String> value = new ArrayList<String>();
        value.add("Original");
        valueList.getValue().addAll(value);
        slotType1.setValueList(valueList);
        slotList.add(slotType1);
        associationType1.getSlot().addAll(slotList);
              
        JAXBElement<AssociationType1> associationType1JAXBElement= 
        		new JAXBElement<AssociationType1>(new QName("urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0","Association"),
        				 (Class<AssociationType1>) associationType1.getClass(),associationType1);
        registryObjectTypeList.add(associationType1JAXBElement);
        
        
        submitObjectsRequest.setRegistryObjectList(registryObjectList);     
        body.setSubmitObjectsRequest(submitObjectsRequest);
        
        
        List<Document> documentList = new ArrayList<Document>();
        Document document = new Document();
        document.setId("Document01");
        //如果是url
//        DataHandler dataHandler = new DataHandler(wsdlUrl);
        
        //如果是文件夹
//        FileDataSource fileDataSource = new FileDataSource(fileSourcePath+"821-2603.pdf"); 
//        FileDataSource fileDataSource = new FileDataSource(fileSourcePath+"评课稿.doc"); 
//        FileDataSource fileDataSource = new FileDataSource(fileSourcePath+"数据质控标准规则配置规范V1.0(3).xlsx"); 
        FileDataSource fileDataSource = new FileDataSource(constProperties.getFileRootPath()+"文档一.txt");
        
        DataHandler dataHandler = new DataHandler(fileDataSource);
 
        log.info(fileDataSource.getName());
        log.info(fileDataSource.getContentType());
        document.setValue(dataHandler);
        documentList.add(document);
        body.getDocument().addAll(documentList);
        RegistryResponseType registryResponseType = myWebService.documentRepositoryProvideAndRegisterDocumentSetB(body);
        log.info("---registryResponseType---" + registryResponseType.getStatus());
        
        
        return registryResponseType;
    }

}